package com.jinzhuo.music.controller;

import com.alibaba.fastjson.JSONObject;
import com.jinzhuo.music.domain.Singer;
import com.jinzhuo.music.domain.SongList;
import com.jinzhuo.music.service.SingerService;
import com.jinzhuo.music.service.SongListService;
import com.jinzhuo.music.utils.Consts;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/songList")
public class SongListController {
    @Autowired
    private SongListService songListService;

    /**
     * 添加歌单
     */
    @RequestMapping(value = "addSongList",method = RequestMethod.POST)
    public Object addSongList(HttpServletRequest request){
        JSONObject object = new JSONObject();
        String title = request.getParameter("title").trim();
        String pic = request.getParameter("pic").trim();
        String introduction = request.getParameter("introduction").trim();
        String style = request.getParameter("style").trim();
        //保存到歌单的对象中
        SongList songList = new SongList();
        songList.setStyle(style);
        songList.setTitle(title);
        songList.setPic(pic);
        songList.setIntroduction(introduction);
        boolean flag = songListService.addSongList(songList);
        if (flag){
            object.put(Consts.CODE,"1");
            object.put(Consts.MSG,"添加成功");
        }else {
            object.put(Consts.CODE,0);
            object.put(Consts.MSG,"添加失败");
        }
        return object;
    }

    /**
     * 修改歌单
     */
    @RequestMapping(value = "updateSongList",method = RequestMethod.POST)
    public Object updateSongList(HttpServletRequest request){
        JSONObject object = new JSONObject();
        String id = request.getParameter("id").trim();
        String title = request.getParameter("title").trim();
//        String pic = request.getParameter("pic").trim();
        String introduction = request.getParameter("introduction").trim();
        String style = request.getParameter("style").trim();
        //保存到歌手的对象中
        SongList songList = new SongList();
        songList.setId(Integer.parseInt(id));
        songList.setTitle(title);
//        songList.setPic(pic);
        songList.setIntroduction(introduction);
        songList.setStyle(style);
        boolean flag = songListService.updateSongList(songList);
        if (flag){
            object.put(Consts.CODE,"1");
            object.put(Consts.MSG,"修改成功");
        }else {
            object.put(Consts.CODE,0);
            object.put(Consts.MSG,"修改失败");
        }
        return object;
    }

    /**
     * 删除
     */
    @RequestMapping(value = "deleteSongList",method = RequestMethod.POST)
    public Object deleteSongList(HttpServletRequest request) {
//        JSONObject object = new JSONObject();
        String id = request.getParameter("id").trim();
        boolean flag = songListService.deleteSongList(Integer.parseInt(id));
        return flag;
    }

    /**
     * 据主键查询
     */
    @RequestMapping(value = "getSongList",method = RequestMethod.POST)
    public Object getSongList(HttpServletRequest request) {
        String id = request.getParameter("id").trim();
        SongList songList = songListService.getSongList(Integer.parseInt(id));
        return songList;
    }

    /**
     * 查询所有歌单
     */
    @RequestMapping(value = "queryAllSongList",method = RequestMethod.POST)
    public Object queryAllSongList(HttpServletRequest request) {
//        String id = request.getParameter("id").trim();
        return songListService.queryAllSongList();
    }

    /**
     * 根据标题精确查询歌单
     */
    @RequestMapping(value = "querySongListByTitle",method = RequestMethod.POST)
    public Object querySongListByTitle(HttpServletRequest request) {
        String title =request.getParameter("title").trim();
        List<SongList> songLists = songListService.querySongListByTitle(title);
        return songLists;
    }

    /**
     * 根据标题模糊查询歌单
     */
    @RequestMapping(value = "querySongListLikeTitle",method = RequestMethod.POST)
    public Object querySingerBySex(HttpServletRequest request) {
        String title =request.getParameter("title").trim();
        List<SongList> songLists = songListService.querySongListLikeTitle("%"+title+"%");
        return songLists;
    }

    /**
     * 根据风格模糊查询歌单
     */
    @RequestMapping(value = "querySongListLikeStyle",method = RequestMethod.POST)
    public Object querySongListLikeStyle(HttpServletRequest request) {
        String style =request.getParameter("style").trim();
        List<SongList> songLists = songListService.querySongListLikeStyle("%"+style+"%");
        return songLists;
    }


    /**
     * 更新歌单图片
     *
     */
    @RequestMapping(value = "updateSongListPic",method = RequestMethod.POST)
    public Object updateSongListPic(@RequestParam("file")MultipartFile avatorFile,@RequestParam("id")int id){
        JSONObject jsonObject = new JSONObject();
        if (avatorFile.isEmpty()){
            jsonObject.put(Consts.CODE,0);
            jsonObject.put(Consts.MSG,"上传失败");
            return jsonObject;
        }
        //文件名=当前时间的毫秒+原来的文件名
        String fileName=System.currentTimeMillis()+avatorFile.getOriginalFilename();
        //文件路径
        String filePath=System.getProperty("user.dir")+System.getProperty("file.separator")+"img"
                +System.getProperty("file.separator")+"songListPic";
        //如果文件路径不存在，新增路径
        File file = new File(filePath);
        if (!file.exists()){
            file.mkdir();
        }
        //实际的文件地址
        File dest = new File(filePath + System.getProperty("file.separator") + fileName);
        //存储到数据库里的相对文件地址
        String storeAvatorPath = "/img/songListPic/"+fileName;
        try {
            avatorFile.transferTo(dest);
            SongList songList = new SongList();
            songList.setId(id);
            songList.setPic(storeAvatorPath);
            boolean flag = songListService.updateSongList(songList);
            if (flag){
                jsonObject.put(Consts.CODE,1);
                jsonObject.put(Consts.MSG,"上传成功");
                return jsonObject;
            }else {
                jsonObject.put(Consts.CODE,0);
                jsonObject.put(Consts.MSG,"上传失败");
                return jsonObject;
            }
        } catch (IOException e) {
            jsonObject.put(Consts.CODE,0);
            jsonObject.put(Consts.MSG,"上传失败"+e.getMessage());
        }finally {
            return jsonObject;
        }
    }
}
